package com.example.ll_video.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.ll_video.MyApplication;
import com.example.ll_video.utils.ToastUtils;
import com.example.ll_video.R;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class ShareActivity extends BaseActivity {
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.btn_save_cord)
    QMUIRoundButton btnSaveCord;
    @BindView(R.id.btn_copy_link)
    QMUIRoundButton btnCopyLink;
    Bitmap bitmap;
    private String qrUrl;
    @Override
    public void onCreate() {
        contentView(R.layout.activity_share_ll);
        ButterKnife.bind(this);
        setTitle("分享");
        showBackImage(v -> finish());
        qrUrl = MyApplication.getInstance().getOtherInfo().getQrcode();
        bitmap = QRCodeEncoder.syncEncodeQRCode(qrUrl, 400);
        ivCode.setImageBitmap(bitmap);
    }

    /**
     * 保存图片
     * @param bitmap
     */
    private void saveScreenShot(Bitmap bitmap) {
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        OutputStream outStream = null;
        String filename;//声明文件名
        //以保存时间为文件名
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        filename = sdf.format(date);
        File file = new File(extStorageDirectory, filename + ".JPEG");//创建文件，第一个参数为路径，第二个参数为文件名
        try {
            outStream = new FileOutputStream(file);//创建输入流
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.close();
//      这三行可以实现相册更新
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            sendBroadcast(intent);//这个广播的目的就是更新图库，发了这个广播进入相册就可以找到你保存的图片了！
            Toast.makeText(ShareActivity.this, "复制成功",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(ShareActivity.this, "失败，exception:" + e,
                    Toast.LENGTH_SHORT).show();

        }

    }

    @OnClick({R.id.btn_save_cord, R.id.btn_copy_link})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save_cord:
                saveScreenShot(bitmap);
                break;
            case R.id.btn_copy_link:
                setClipboard(qrUrl);
                break;
        }

    }

    /**
     * 复制
     * @param text
     */
    public void setClipboard(String text) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", text);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        ToastUtils.showTextToast(context,"复制成功");
    }
}